#include <Reg51.h>
#include "delay.h"
#include "LCD1602.h"
#include "Key.h"

void StartMenu();
void SubmenuOne();
void SubmenuTwo();
void SubmenuThree();
void SubmenuFour();
void AlarmSetMenu();
void ShowTempData(unsigned char x,unsigned char y,int *buf,unsigned char num);
void KeyService();
void LCDShowService();

int TEMPbuf[] = { 125,126,124,126,223,224,225,226,335,336,337,338,441,442,443,444 };
unsigned char menu;

void main()
{
	LCDInit();
	DelayXms(10);
	StartMenu();
	DelayXms(2000);
	LCDWriteCmd(LCD_SCREEN_CLR);
	while(1)
	{
		KeyService();
		LCDShowService();
	}
}

void KeyService()
{
	switch(Key_Scan())
	{
		case 1:menu++;if(menu == 4) menu = 0;LCDWriteCmd(LCD_SCREEN_CLR);break;
		case 2:menu--;if(menu > 4) menu = 0;LCDWriteCmd(LCD_SCREEN_CLR);break;
		case 3:menu = 0;LCDWriteCmd(LCD_SCREEN_CLR);break;
		case 4:menu = 4;LCDWriteCmd(LCD_SCREEN_CLR);break;
		default:break;
	}
}

void LCDShowService()
{
	switch(menu)
	{
		case 0:SubmenuOne();break;
		case 1:SubmenuTwo();break;
		case 2:SubmenuThree();break;
		case 3:SubmenuFour();break;
		case 4:AlarmSetMenu();break;
		default:break;
	}
}

void StartMenu()
{
	LCDShowStr(1,0,"The DAS of TEMP");
	LCDShowStr(6,1,"V1.0");
}

void SubmenuOne()
{
	LCDShowStr(0,0,"CH0:");
	DelayXms(300);
	ShowTempData(4,0,TEMPbuf,0);
	LCDShowStr(8,0,"CH2:");
	DelayXms(300);
	ShowTempData(12,0,TEMPbuf,2);
	LCDShowStr(0,1,"CH1:");
	DelayXms(300);
	ShowTempData(4,1,TEMPbuf,1);
	LCDShowStr(8,1,"CH3:");
	DelayXms(300);
	ShowTempData(12,1,TEMPbuf,3);
}

void SubmenuTwo()
{
	LCDShowStr(0,0,"CH4:");
	DelayXms(300);
	ShowTempData(4,0,TEMPbuf,4);
	LCDShowStr(8,0,"CH6:");
	DelayXms(300);
	ShowTempData(12,0,TEMPbuf,6);
	LCDShowStr(0,1,"CH5:");
	DelayXms(300);
	ShowTempData(4,1,TEMPbuf,5);
	LCDShowStr(8,1,"CH7:");
	DelayXms(300);
	ShowTempData(12,1,TEMPbuf,7);
}

void SubmenuThree()
{
	LCDShowStr(0,0,"CH8:");
	DelayXms(300);
	ShowTempData(4,0,TEMPbuf,8);
	LCDShowStr(8,0,"CHA:");
	DelayXms(300);
	ShowTempData(12,0,TEMPbuf,10);
	LCDShowStr(0,1,"CH9:");
	DelayXms(300);
	ShowTempData(4,1,TEMPbuf,9);
	LCDShowStr(8,1,"CHB:");
	DelayXms(300);
	ShowTempData(12,1,TEMPbuf,11);
}

void SubmenuFour()
{
	LCDShowStr(0,0,"CHC:");
	DelayXms(300);
	ShowTempData(4,0,TEMPbuf,12);
	LCDShowStr(8,0,"CHE:");
	DelayXms(300);
	ShowTempData(12,0,TEMPbuf,14);
	LCDShowStr(0,1,"CHD:");
	DelayXms(300);
	ShowTempData(4,1,TEMPbuf,13);
	LCDShowStr(8,1,"CHF:");
	DelayXms(300);
	ShowTempData(12,1,TEMPbuf,15);
}

void ShowTempData(unsigned char x,unsigned char y,int *buf,unsigned char num)
{
	LCDSetPosition(x,y);
	LCDWriteData(*(buf + num) / 100 + '0');
	LCDWriteData(*(buf + num) / 10 % 10 + '0');
	LCDWriteData('.');
	LCDWriteData(*(buf + num) % 10 + '0');
}

void AlarmSetMenu()
{
	LCDShowStr(0,0,"   ");
	LCDShowStr(4,0,"ALARM MENU");
}